package lagou.work3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.*;

/**
 * 客户端类
 */
public class Client {
    private Socket socket = null;
    private PrintStream printStream = null;
    private BufferedReader bufferedReader;
    private Scanner scanner;
    //用户名标识
    private String user;
    public static ExecutorService executor = Executors.newCachedThreadPool();


    public Client(String user) {
        try {
            socket = new Socket("127.0.0.1", 9999);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.user = user;
    }

    /**
     * 发送消息
     */
    public void send() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("连接服务器成功");
                    scanner = new Scanner(System.in);
                    printStream = new PrintStream(socket.getOutputStream());
                    while (true) {
                        String strSend = scanner.next();
                        printStream.println(user + ":" + strSend);
                        System.out.println("发送成功");

                        if ("bye".equalsIgnoreCase(strSend)) {
                            System.out.println("您已退出聊天室");
                            release();
                            break;

                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 获得服务器返回
     */
    public void get() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String stringBacK = null;
                    try {
                        bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        stringBacK = bufferedReader.readLine();
                        if (stringBacK != null) {
                            int z = stringBacK.indexOf(":");
                            if (stringBacK.substring(5, z).equals(user)) {
                                stringBacK = stringBacK.replace(stringBacK.substring(0, z), "我");
                            }
                            System.out.println(stringBacK);
                        }else {
                            break;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }
        });
    }

    /**
     * 资源释放
     */
    public void release() {
        try {
            if (null != bufferedReader) {
                bufferedReader.close();
            }
            if (null != printStream) {
                printStream.close();
            }
            if (null != scanner) {
                scanner.close();
            }
            if(null!=socket){
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.exit(0);
    }
}

